package cn.kiliter.serviece;

import cn.kiliter.dao.ProductDao;
import cn.kiliter.model.PageResult;
import cn.kiliter.model.Product;

import java.sql.SQLException;
import java.util.List;

public class ProductService {
    private ProductDao dao=new ProductDao();
    public PageResult<Product>findPageBooks(int currentPage,int pageSize,String category ){
        //创建PageResult对象
        PageResult<Product> pageResult=new PageResult<>();
        //设置每页显示数
        pageResult.setPageSize(4);
        //设置当前页
        pageResult.setCurrentPage(currentPage);
        //设置总条数
        try {
            long totalCount=dao.findTotalCount(category);
            pageResult.setTotalCount(totalCount);
            //设置总页数
            int totalPage=(int)Math.ceil(totalCount* 1.0 / pageSize);
            pageResult.setTotalPage(totalPage);
            //设置List
            List<Product> list =dao.findList(currentPage,pageSize,category);
            pageResult.setList(list);
            return pageResult;
        } catch (SQLException e) {
            e.printStackTrace();

        }
        return null;
    }


    public Product findProDuctById(int id) {
        try {
            Product product= dao.findProDuctById(id);
            if(product!=null){
                return product;
            }
        } catch (SQLException e) {
            e.printStackTrace();

        }
        return null;
    }
}
